package com.youka.bussiness.common.poi;


import com.youka.bussiness.common.base.BaseException;
import com.youka.bussiness.common.poi.entity.Student;
import com.youka.bussiness.common.utils.ReflectUtil;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.ss.usermodel.HorizontalAlignment;

import java.io.File;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

public class ExportExcel {

    public static final String exportPath = "/home/excel/";
    //    public static final String exportPath = "/E:/excel/";

    private static List<Student> getStudent() throws Exception {
        List list = new ArrayList();
        SimpleDateFormat df = new SimpleDateFormat("yyyy-mm-dd");

        //        Student user1 = new Student(1, "张三", 16, "1997-03-12");
        //        Student user2 = new Student(2, "李四", 17, "1996-08-12");
        //        Student user3 = new Student(3, "王五", 26, "1985-11-12");
        //        list.add(user1);
        //        list.add(user2);
        //        list.add(user3);

        return list;
    }


    public static String export(String tableName, String tableCell, List<?> params) {
        String path = exportPath + System.currentTimeMillis() + ".xls";
        try {
            File file = new File(exportPath);
            if (!file.exists()) {
                file.mkdirs();
            }
            // 第一步，创建一个webbook，对应一个Excel文件
            HSSFWorkbook wb = new HSSFWorkbook();
            // 第二步，在webbook中添加一个sheet,对应Excel文件中的sheet
            HSSFSheet sheet = wb.createSheet(tableName);
            // 第三步，在sheet中添加表头第0行,注意老版本poi对Excel的行数列数有限制short
            HSSFRow row = sheet.createRow(0);
            // 第四步，创建单元格，并设置值表头 设置表头居中
            HSSFCellStyle style = wb.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER); // 创建一个居中格式
            String[] rows = tableCell.split(",");
            HSSFCell cell = row.createCell((short) 0);
            cell.setCellValue(rows[0]);
            cell.setCellStyle(style);
            int len = rows.length;
            for (int i = 1; i < len; i++) {
                cell = row.createCell((short) i);
                cell.setCellValue(rows[i]);
                cell.setCellStyle(style);
            }

            // 第五步，写入实体数据 实际应用中这些数据从数据库得到，
            for (int i = 0; i < params.size(); i++) {
                row = sheet.createRow(i + 1);
                List<?> objects = ReflectUtil.getObjectValueToList(params.get(i), new ArrayList());
                // 第四步，创建单元格，并设置值
                for (int j = 0; j < len; j++) {
                    row.createCell((short) j).setCellValue(String.valueOf(objects.get(j)));
                }
            }

            // 第六步，将文件存到指定位置
            FileOutputStream out = new FileOutputStream(path);
            wb.write(out);
            out.close();
        } catch (Exception e) {
            throw new BaseException("101", "文件导出失败");
        }
        return path;
    }
}